home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_108 / bash-108.zoo / bash-1.08 / dispose_cmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-22  |  3.7 KB  |  182 lines

  1. /* dispose_command.c -- dispose of a COMMAND structure. */
  2.  
  3. /* Copyright (C) 1987,1991 Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Bash, the Bourne Again SHell.
  6.  
  7.    Bash is free software; you can redistribute it and/or modify it
  8.    under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 1, or (at your option)
  10.    any later version.
  11.  
  12.    Bash is distributed in the hope that it will be useful, but WITHOUT
  13.    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14.    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  15.    License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with Bash; see the file COPYING.  If not, write to the Free
  19.    Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. #include "shell.h"
  22.  
  23. /* Dispose of the command structure passed. */
  24. dispose_command (command)
  25.      register COMMAND *command;
  26. {
  27.   if (!command) return;
  28.  
  29.   if (command->redirects)
  30.     dispose_redirects (command->redirects);
  31.  
  32.   switch (command->type)
  33.     {
  34.     case cm_for:
  35.       {
  36.     register FOR_COM *c = command->value.For;
  37.     dispose_word (c->name);
  38.     dispose_words (c->map_list);
  39.     dispose_command (c->action);
  40.     free (c);
  41.     break;
  42.       }
  43.     
  44.     case cm_group:
  45.       {
  46.     dispose_command (command->value.Group->command);
  47.     break;
  48.       }
  49.  
  50.     case cm_case:
  51.       {
  52.     register CASE_COM *c = command->value.Case;
  53.     PATTERN_LIST *t, *p = c->clauses;
  54.     dispose_word (c->word);
  55.     while (p) {
  56.       dispose_words (p->patterns);
  57.       dispose_command (p->action);
  58.       t = p;
  59.       p = p->next;
  60.       free (t);
  61.     }
  62.     break;
  63.       }
  64.  
  65.     case cm_until:
  66.     case cm_while:
  67.       {
  68.     register WHILE_COM *c = command->value.While;
  69.     dispose_command (c->test);
  70.     dispose_command (c->action);
  71.     free (c);
  72.     break;
  73.       }
  74.  
  75.     case cm_if:
  76.       {
  77.     register IF_COM *c = command->value.If;
  78.     dispose_command (c->test);
  79.     dispose_command (c->true_case);
  80.     dispose_command (c->false_case);
  81.     free (c);
  82.     break;
  83.       }
  84.  
  85.     case cm_simple:
  86.       {
  87.     register SIMPLE_COM *c = command->value.Simple;
  88.     dispose_words (c->words);
  89.     dispose_redirects (c->redirects);
  90.     free (c);
  91.     break;
  92.       }
  93.  
  94.     case cm_connection:
  95.       {
  96.     register CONNECTION *c = command->value.Connection;
  97.     dispose_command (c->first);
  98.     dispose_command (c->second);
  99.     free (c);
  100.     break;
  101.       }
  102.  
  103.     case cm_function_def:
  104.       {
  105.     register FUNCTION_DEF *c = command->value.Function_def;
  106.     dispose_word (c->name);
  107.     dispose_command (c->command);
  108.     free (c);
  109.     break;
  110.       }
  111.  
  112.     default:
  113.       report_error ("Attempt to free unknown command type `%d'.\n", command->type);
  114.       break;
  115.     }
  116.   free (command);
  117. }
  118.  
  119. /* How to free a WORD_DESC. */
  120. dispose_word (word)
  121.      WORD_DESC *word;
  122. {
  123.   free (word->word);
  124.   free (word);
  125. }
  126.  
  127. /* How to get rid of a linked list of words.  A WORD_LIST. */
  128. dispose_words (list)
  129.      WORD_LIST *list;
  130. {
  131.   WORD_LIST *t;
  132.   while (list)
  133.     {
  134.       t = list;
  135.       list = list->next;
  136.       dispose_word (t->word);
  137.       free (t);
  138.     }
  139. }
  140.  
  141. /* How to dispose of an array of pointers to char. */
  142. dispose_word_array (array)
  143.      char **array;
  144. {
  145.   int count = 0;
  146.  
  147.   for (count = 0; array[count]; count++)
  148.     free (array[count]);
  149.  
  150.   free (array);
  151. }
  152.  
  153. /* How to dispose of an list of redirections.  A REDIRECT. */
  154. dispose_redirects (list)
  155.      REDIRECT *list;
  156. {
  157.   register REDIRECT *t;
  158.  
  159.   while (list)
  160.     {
  161.       t = list;
  162.       list = list->next;
  163.       switch (t->instruction)
  164.     {
  165.     case r_reading_until:
  166.     case r_deblank_reading_until:
  167.       free (t->here_doc_eof);
  168.       /* ... */
  169.     case r_output_direction:
  170.     case r_input_direction:
  171.     case r_inputa_direction:
  172.     case r_appending_to:
  173.     case r_err_and_out:
  174.     case r_input_output:
  175.     case r_output_force:
  176.       dispose_word (t->redirectee.filename);
  177.       break;
  178.     }
  179.       free (t);
  180.     }
  181. }
  182.